package com.example.driving.ui;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.FragmentTransaction;

import com.example.driving.R;
import com.example.driving.base.BaseActivity;
import com.example.driving.databinding.ActivityUserBinding;
import com.example.driving.ui.user.FindPasswordFragment;
import com.example.driving.ui.user.LoginFragment;
import com.example.driving.ui.user.RegisterFragment;
import com.example.driving.ui.user.UserFragmentEvent;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

public class UserActivity extends BaseActivity<ActivityUserBinding> {

    protected LoginFragment loginFragment;
    protected RegisterFragment registerFragment;
    protected FindPasswordFragment findPasswordFragment;
    ActivityUserBinding mBinding;
    private int position;
    @Override
    protected ActivityUserBinding getViewBinding() {
        mBinding = ActivityUserBinding.inflate(getLayoutInflater());
        return mBinding;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setFragmentIndex(0);
        EventBus.getDefault().register(this);
    }

    public void setFragmentIndex(int position){
        this.position = position;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position){
            case 0:{
                if (loginFragment == null){
                    loginFragment = new LoginFragment();
                }
                transaction.replace(R.id.view_user,loginFragment);
                break;
            }
            case 1:{
                if (registerFragment == null){
                    registerFragment = new RegisterFragment();
                }
                transaction.replace(R.id.view_user,registerFragment);
                break;
            }
            case 2:{
                if (findPasswordFragment == null){
                    findPasswordFragment = new FindPasswordFragment();
                }
                transaction.replace(R.id.view_user,findPasswordFragment);
                break;
            }
        }
        transaction.commitAllowingStateLoss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFragmentEvent(UserFragmentEvent fragmentEvent){
        try {
            setFragmentIndex(fragmentEvent.getIndex());
        } catch (Exception e) {
            Logger.t("EventBus").e(Objects.requireNonNull(e.getMessage()));
        }

    }

    @Override
    protected void initData() {
        mBinding.ivBack.setOnClickListener(v -> {
            if (position!=0){
                setFragmentIndex(0);
                return;
            }
            finish();
        });
    }

    @Override
    public void onBaseClick(View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}